import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

from ixapi import SimpleListDB

class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('Hello world!')

class SimpleList(webapp.RequestHandler):

  def get(self):
    listDb = SimpleListDB.all().order('-created')
    view_list = os.path.join(os.path.dirname(__file__), 'view/apiview/SimpleList.htm')
    view_list_values = {
      'lists': listDb,
      }
    self.response.out.write(template.render(view_list, view_list_values))
    
class SimpleListAdd(webapp.RequestHandler):

  def post(self):
    listDb = SimpleListDB()
    listDb.title = self.request.get('title')
    listDb.put()
    self.redirect('/apimanager/simplelist')

def main():
  application = webapp.WSGIApplication([('/apimanager', MainHandler),
                                    ('/apimanager/', MainHandler),
                                    ('/apimanager/simplelist', SimpleList),
                                    ('/apimanager/simplelistadd', SimpleListAdd)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
